// signatureUtils.js
import CryptoJS from 'crypto-js';

export async function generateSignature(token, appId, secretKey) {
  // console.log('token:', token)
  // 获取当前时间戳（秒级）
  const timestamp =  Math.floor(Date.now() / 1000 / 60);

  const useTimestamp = window.HC_config.VUE_USE_TIMESTAMP || false;

  // 构造签名输入字符串
  const signatureInput = useTimestamp ? `${appId}|${timestamp}|${token}` : `${appId}|${token}`;
  //const signatureInput = `${appId}|${token}`

  // 生成 HMAC SHA-256 签名
  const signature = await calculateHmac(signatureInput, secretKey)

  // console.log('signature:', signature)
  return {
    signature
  }
}

function calculateHmac(data, secretKey) {
  console.log('data:', data);
  const hmac = CryptoJS.HmacSHA256(data, secretKey);
  return hmac.toString(CryptoJS.enc.Base64);
}
